import http from '@/utils/request'
/**
 * 获取商品的specs和skus
 * @param {String} skuId - 商品SKUID
 * @returns Promise
 */
export const getSpecsAndSkus = (skuId) => {
    return http(`/goods/sku/${skuId}`, 'get')
}

/**
 * 合并本地购物车
 * @param {Array<object>} cartList - 本地购物车数组 [{skuId:1,count:2,selected:false},{skuId:2,count:21,selected:false}]
 * @param {String} item.skuId - 商品SKUID
 * @param {Boolean} item.selected - 是否选中
 * @param {Integer} item.count - 数量
 */
export const mergeLocalCart = (cartList) => {
    return http('/member/cart/merge', 'post', cartList)
}

/**
 * 获取登录后的购物车列表
 * @returns Promise
 */
export const getCartList = () => {
    return http('/member/cart', 'get')
}

/**
 * 登录后加入购物车
 * @param {String} skuId - 商品SKUID
 * @param {Integer} count - 商品数量
 * @returns Promise
 */
export const insertCart = ({ skuId, count }) => {
    return http('/member/cart', 'post', { skuId, count })
}

/**
 * 登录后删除商品（支持批量删除）
 * @param {Array<string>} ids - skuId集合 ['200028','2282882']
 * @returns Promise
 */
export const deleteCart = (ids) => {
    return http('/member/cart', 'delete', { ids })
}

/**
 * 登录修改购物车商品的状态和数量
 * @param {String} goods.skuId - 商品sku
 * @param {Boolean} goods.selected - 选中状态
 * @param {Integer} goods.count - 商品数量
 * @returns Promise
 */
export const updateCart = (goods) => {
    return http('/member/cart/' + goods.skuId, 'put', goods)
}

/**
 * 登录后的全选反选
 * @param {Boolean} selected - 选中状态
 * @param {Array<string>} ids - 有效商品skuId集合
 * @returns Promise
 */
export const checkAllCart = ({ selected, ids }) => {
    return http('/member/cart/selected', 'put', { selected, ids })
}

/**
 * 获取最新的购物车商品的最新信息(本地购物车，未登录情况)
 * @param {String} skuId - 商品SKUID
 * @returns Promise
 */
export const getNewCartGoods = skuId => {
    return http(`/goods/stock/${skuId}`, 'get')
}